package com.play.ballen.me.ui

import android.content.Context
import android.content.Intent
import android.text.InputType
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.config.KVConst
import com.play.ballen.databinding.ActivityLoginBinding
import com.play.ballen.home.ui.AddInformationActivity
import com.play.ballen.home.ui.MainActivity
import com.play.ballen.me.vm.LoginVM
import com.yariksoffice.lingver.Lingver
import com.yuven.appframework.util.MMKVUtil
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.component.BaseMVVMActivity
import com.yuven.baselib.utils.ResourceUtil

class LoginActivity : BaseMVVMActivity<LoginVM, ActivityLoginBinding>() {

    private val countDownTip by lazy { ResourceUtil.getString(R.string.count_down_time_tip) }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, LoginActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun initView() {

    }


    override fun initObserver() {
        super.initObserver()

        viewModel.actionLoginSuccess.observe(this) {

        }
        viewModel.imLoginStatus.observe(this){
            if (it==1){

                var photo = MMKVUtil.decodeString(KVConst.PHOTO,false)?:""
                var name = MMKVUtil.decodeString(KVConst.NICK_NAME,false)?:""

                if(photo.isEmpty()||name.isEmpty()){
                    AddInformationActivity.start(mContext)
                }else{
                    val account = dataBinding.edAccount.text.toString().trim()
                    MMKVUtil.encode(KVConst.KEY_ACCOUNT_REMEMBER, account, true)
                    MainActivity.start(mContext)
                }

            }
        }
        viewModel.actionCountDown.observe(this) {
            dataBinding.tvGetCode.isClickable = false
            dataBinding.tvGetCode.isEnabled = false
            dataBinding.tvGetCode.text = it.toString() + countDownTip
        }
        viewModel.actionCountDownComplete.observe(this) {
            dataBinding.tvGetCode.isClickable = true
            dataBinding.tvGetCode.isEnabled = true
            dataBinding.tvGetCode.text = ResourceUtil.getString(R.string.register_code_get)
        }
    }

    override fun layoutId(): Int {
        return  R.layout.activity_login
    }

    override fun viewModelClass(): Class<LoginVM> {
        return LoginVM::class.java
    }

    override fun initData() {
        val account = MMKVUtil.decodeString(KVConst.KEY_ACCOUNT_REMEMBER, true)
        val pwd = MMKVUtil.decodeString(KVConst.KEY_PWD_REMEMBER, true)
        if (!account.isNullOrBlank()) {
            dataBinding.edAccount.setText(account)
        }
        dataBinding.layoutTitle.ivBtnBack.setOnClickListener{
            finish()
        }
        val localeListCompat = Lingver.getInstance().getLanguage()

        dataBinding.tvGetCode.setOnSingleClickListener tag@{
            val account = dataBinding.edAccount.text.toString().trim()
            if (account.isEmpty()) {
                ToastUtils.show(dataBinding.edAccount.hint)
                return@tag
            }
            viewModel.sendCode(account)
        }

        dataBinding.btnLogin.setOnSingleClickListener tag@{
            if(!dataBinding.checkbox.isChecked){
                ToastUtils.show("请认真阅读用户协议并同意")
                return@tag
            }
            val account = dataBinding.edAccount.text.toString().trim()
            if (account.isEmpty()) {
                ToastUtils.show(dataBinding.edAccount.hint)
                return@tag
            }

            val code = dataBinding.edCode.text.toString().trim()
            if (code.isEmpty()) {
                ToastUtils.show(dataBinding.edCode.hint)
                return@tag
            }

            viewModel.login(account, code)
        }
    }

    override fun initIntent(intent: Intent?) {
    }



}